<template>
  <div class="app-container">
    <!--        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="排放口名称" prop="noName">
              <el-input
                v-model="queryParams.noName"
                placeholder="请输入排放口名称"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="噪声类别" prop="noWasteType">
              <el-select v-model="queryParams.noWasteType" placeholder="请选择噪声类别" clearable size="small">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </el-form-item>
            <el-form-item label="排放周期" prop="noEmissionType">
              <el-select v-model="queryParams.noEmissionType" placeholder="请选择排放周期" clearable size="small">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </el-form-item>
            <el-form-item label="经度" prop="noLg">
              <el-input
                v-model="queryParams.noLg"
                placeholder="请输入经度"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="纬度" prop="noLt">
              <el-input
                v-model="queryParams.noLt"
                placeholder="请输入纬度"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="照片" prop="noPicture">
              <el-input
                v-model="queryParams.noPicture"
                placeholder="请输入照片"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="浓度" prop="noConcentration">
              <el-input
                v-model="queryParams.noConcentration"
                placeholder="请输入浓度"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="总量" prop="noTotal">
              <el-input
                v-model="queryParams.noTotal"
                placeholder="请输入总量"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="当地环境噪声质量标准" prop="noEnvNoiseStandard">
              <el-input
                v-model="queryParams.noEnvNoiseStandard"
                placeholder="请输入当地环境噪声质量标准"
                clearable
              size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="日间噪声标准" prop="noDayStandard">
              <el-input
                v-model="queryParams.noDayStandard"
                placeholder="请输入日间噪声标准"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="夜间噪声排放标准" prop="noNightStandard">
              <el-input
                v-model="queryParams.noNightStandard"
                placeholder="请输入夜间噪声排放标准"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="是否有在线监测设施(0代表存在 1代表不存在)" prop="noHasOnlineFacility">
              <el-input
                v-model="queryParams.noHasOnlineFacility"
                placeholder="请输入是否有在线监测设施(0代表存在 1代表不存在)"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="监督性监测报告" prop="noReport">
              <el-input
                v-model="queryParams.noReport"
                placeholder="请输入监督性监测报告"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="环境监察记录" prop="noRecord">
              <el-input
                v-model="queryParams.noRecord"
                placeholder="请输入环境监察记录"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['pollutant:noise:add']"
          icon="el-icon-plus"
          plain
          size="small"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['pollutant:noise:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="small"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['pollutant:noise:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="small"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['pollutant:noise:export']"
          :loading="exportLoading"
          icon="el-icon-download"
          plain
          size="small"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noiseList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="噪音产生单元" prop="noName"/>
      <!--      <el-table-column align="center" label="噪声类别" prop="noWasteType"/>-->
      <!--      <el-table-column align="center" label="经度" prop="noLg"/>-->
      <!--      <el-table-column align="center" label="纬度" prop="noLt"/>-->
      <!--      <el-table-column align="center" label="照片" prop="noPicture"/>-->
      <el-table-column align="center" label="浓度" prop="noConcentration"/>
      <el-table-column align="center" label="总量" prop="noTotal"/>
      <!--      <el-table-column align="center" label="当地环境噪声质量标准" prop="noEnvNoiseStandard"/>-->
      <!--      <el-table-column align="center" label="日间噪声标准" prop="noDayStandard"/>-->
      <!--      <el-table-column align="center" label="夜间噪声排放标准" prop="noNightStandard"/>-->
      <!--      <el-table-column align="center" label="是否有在线监测设施(0代表存在 1代表不存在)" prop="noHasOnlineFacility"/>-->
      <el-table-column align="center" label="监督性监测报告" prop="noReport">
        <template slot-scope="scope" v-if="scope.row.noReport">
          <a :href="`${baseUrl}${scope.row.noReport}`" target="_blank"
             class="buttonText">{{ scope.row.noReport.toString().substring(scope.row.noReport.lastIndexOf("/") + 1) }}</a>
        </template>
      </el-table-column>
      <el-table-column align="center" label="环境监察记录" prop="noRecord">
        <template slot-scope="scope" v-if="scope.row.noRecord">
          <a :href="`${baseUrl}${scope.row.noRecord}`" target="_blank"
             class="buttonText">{{ scope.row.noRecord.toString().substring(scope.row.noRecord.lastIndexOf("/") + 1) }}</a>
        </template>
      </el-table-column>
      <!--      <el-table-column align="center" label="备注" prop="remark"/>-->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['pollutant:noise:edit']"
            size="small"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['pollutant:noise:remove']"
            size="small"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改噪声排放对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="70%">
      <el-form ref="form" :model="form" :rules="rules" class="clearfix m-p-n-i-1" label-width="110px"
               style="padding-left: 20px; padding-right: 20px;">
        <div class="content-left" style="width: 45%">
          <el-form-item label="噪音产生单元" prop="noName">
            <el-input v-model="form.noName" placeholder="请输入排放口名称"/>
          </el-form-item>
          <!--          <el-form-item label="噪声类别" prop="noWasteType">-->
          <!--            <el-select v-model="form.noWasteType" placeholder="请选择噪声类别" style="width: 100%">-->
          <!--              <el-option label="请选择字典生成" value=""/>-->
          <!--            </el-select>-->
          <!--          </el-form-item>-->
          <el-form-item label="排放周期" prop="noEmissionType">
            <el-date-picker type="daterange" v-model="form.noEmissionType" format="yyyy-MM-dd"
                            value-format="yyyy-MM-dd" :style="{width: '100%'}" start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            range-separator="至" clearable>
            </el-date-picker>
          </el-form-item>
<!--          <el-form-item label="浓度" prop="noConcentration">
            <el-input v-model="form.noConcentration" placeholder="请输入浓度"/>
          </el-form-item>-->
          <el-form-item label="夜间排放标准" prop="noNightStandard">
            <el-input v-model="form.noNightStandard" placeholder="请输入夜间排放标准"/>
          </el-form-item>
          <el-form-item label="日间排放标准" prop="noDayStandard">
            <el-input v-model="form.noDayStandard" placeholder="请输入日间噪声标准"/>
          </el-form-item>
          <el-form-item label="监测点位坐标">
            <el-row>
              <el-col>
                <div style="display: flex">
                  <div style="width: 38%">
                    <el-input v-model="form.noLg" placeholder="请输入经度"/>
                  </div>
                  <div style="width: 38%">
                    <el-input v-model="form.noLt" placeholder="请输入纬度"/>
                  </div>
                </div>
              </el-col>
            </el-row>
          </el-form-item>
        </div>
        <div class="content-left" style="width: 45%; float:right">
          <el-form-item label="监督性监测报告" prop="noReport">
            <FileUpload v-model="form.noReport"/>
          </el-form-item>
          <el-form-item label="监测时间" prop="remark">
            <el-date-picker v-model="form.remark" clearable
                            placeholder="选择监测时间"
                            size="small"
                            style="width:100%"
                            type="date"
                            value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="是否超标" prop="updateBy">
            <el-select v-model="form.updateBy" placeholder="请选择" @change="changeOK">
              <el-option
                v-for="dict in typeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="环境监察记录" prop="noRecord">
            <FileUpload v-model="form.noRecord"/>
          </el-form-item>
          <el-form-item label="噪声质量标准" prop="noEnvNoiseStandard">
            <el-input v-model="form.noEnvNoiseStandard" placeholder="请输入当地环境噪声质量标准"/>
          </el-form-item>
          <el-form-item label="是否有关联在线监测" prop="noHasOnlineFacility">
            <div style="width: 100%;">
              <el-select v-model="zxIndex" clearable placeholder="请选择" style="width: 100%;"
                         @change="zxChange">
                <el-option v-for="item in zx" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </el-form-item>
<!--          <el-form-item label="总量" prop="noTotal">
            <el-input v-model="form.noTotal" placeholder="请输入总量"/>
          </el-form-item>-->
          <el-form-item label="监测点位照片" prop="noPicture">
            <ImageUpload v-model="form.noPicture"/>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addNoise, delNoise, exportNoise, getNoise, listNoise, updateNoise} from "@/api/pollutant/noise";
import {getToken} from "@/utils/auth";
import {InfoItem} from "@/api/enterprise/info";
import {isNotMUstTwoPoint,validatePhoneTwo,validateIdNo,validateEMail} from "@/api/validate/validate";


export default {
  name: "Noise",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      zx: [
        {
          value: '0',
          label: '有'
        },
        {
          value: '1',
          label: '无'
        }
      ],
      zxIndex: '1',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 噪声排放表格数据
      noiseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noName: null,
        noWasteType: null,
        noEmissionType: null,
        noLg: null,
        noLt: null,
        noPicture: null,
        noConcentration: null,
        noTotal: null,
        noEnvNoiseStandard: null,
        noDayStandard: null,
        noNightStandard: null,
        noHasOnlineFacility: null,
        noReport: null,
        noRecord: null,
        epId:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        noName: [
          {required: true, message: "排放口名称不能为空", trigger: "blur"}
        ],
        noWasteType: [
          {required: true, message: "噪声类别不能为空", trigger: "change"}
        ],
        noTotal: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
      },
      typeOptions:[],
    };
  },
  created() {
    this.getDicts("common_whether").then(response => {
      this.typeOptions = response.data;
    });
    this.getList();
  },
  methods: {
    changeOK(e){
      if(e == 0){
        this.guolu = true;
      }
      if(e == 1){
        this.guolu = false;
      }
    },
    zxChange(e) {
      this.form.noHasOnlineFacility = e;
    },
    /** 查询噪声排放列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        if (res.hasOwnProperty("data")) {
          this.queryParams.epId = res.data.epId;
          listNoise(this.queryParams).then(response => {
            this.noiseList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noId: null,
        noName: null,
        noWasteType: null,
        noEmissionType: null,
        noLg: null,
        noLt: null,
        noPicture: null,
        noConcentration: null,
        noTotal: null,
        noEnvNoiseStandard: null,
        noDayStandard: null,
        noNightStandard: null,
        noHasOnlineFacility: null,
        noReport: null,
        noRecord: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        epId:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加噪声排放";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const noId = row.noId || this.ids
      getNoise(noId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改噪声排放";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.noId != null) {
            updateNoise(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")) {
                this.form.epId = res.data.epId;
              }
              addNoise(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noIds = row.noId || this.ids;
      this.$confirm('是否确认删除噪声排放编号为"' + noIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delNoise(noIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有噪声排放数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportNoise(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
